package sxe.core.math;

import snobject.snDouble;
import snobject.snObject;
import snobject.snObjectArgList;
import step.FunctionWrapper;
import step.EvaluationFailure;
import sxe.core.EvaluationFailureReasons;

/**
 * Converts rectangular coordinates (x, y) to polar (r, theta).
 * This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi.
 * @Param argv must contain two snDouble objects at top of the stack
 * @return returns the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.
 */

public class atan2 extends FunctionWrapper{
	
	public snObject Call(snObjectArgList argv) throws EvaluationFailure{
		// REQUIRES: argv must contain two snDouble object at top of the stack
		// EFFECTS:	if argv contains less than 2 objects, throw EvaluationFailure exception
		//			otherwise returns the snDouble of the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates
		if(argv.length()<2)
			throw new EvaluationFailure(super.expnode,
					EvaluationFailureReasons.BAD_PARAMETER_COUNT);
		
		double ret1 = argv.popDouble().getDbl();
		double ret2 = argv.popDouble().getDbl();
		
		return new snDouble(Math.atan2(ret1, ret2));
	}
}